﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Fc
{
    public partial class 产品判定入库 : System.Web.UI.Page
    {
        protected bool IsReadonly = false;
        protected string Notice = "";
        protected decimal ZZL = 0;
        protected decimal ZCD = 0;
        protected int ZZS = 0;
        protected int ZJS = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            int bid=0,did=0;
            if (!this.IsPostBack)
            {

                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out bid);
                }

                if (bid > 0)
                {
                    this.Label_ID.Text = bid.ToString();
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;

                    Cmd.CommandText = "select 序号,产品类别ID,生产线,日期,库区,总件数,总支数,总重量,总长度,制表人,备注,状态,是否合格,判定结论,质检员 from 产品入库单 where ID=" + bid;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        if (did != 0 && !((short)Reader["生产线"]).ToString().StartsWith(did.ToString()))
                        {
                            this.Notice = "您不能越权查看数据";
                        }
                        else
                        {
                            this.XuHao.Text = ((int)Reader["序号"]).ToString();
                            this.ChanPin.Text = ((int)Reader["产品类别ID"]).ToString();
                            this.FenChang.Text = ((short)Reader["生产线"]).ToString();
                            this.Date1.Text = ((DateTime)Reader["日期"]).ToLongDateString();
                            this.KuQu.Text = ((byte)Reader["库区"]).ToString();
                            this.ZJS = (byte)Reader["总件数"];
                            this.ZZS = (short)Reader["总支数"];
                            this.ZZL = (decimal)Reader["总重量"];
                            this.ZCD = (decimal)Reader["总长度"];
                            if (!Convert.IsDBNull(Reader["制表人"]))
                            {
                                this.Label_ZhiBiao.Text = (String)Reader["制表人"];
                            }
                            if (!Convert.IsDBNull(Reader["备注"]))
                            {
                                this.BeiZhu.Value = (String)Reader["备注"];
                            }
                            if (!Convert.IsDBNull(Reader["判定结论"]))
                            {
                                this.Label_JieLun.Text = (String)Reader["判定结论"];
                            }
                            if (!Convert.IsDBNull(Reader["质检员"]))
                            {
                                this.Label_PanDing.Text = (String)Reader["质检员"];
                            }

                            if ((bool)Reader["是否合格"] || (byte)Reader["状态"]>0)
                            {
                                if ((bool)Reader["是否合格"])
                                {
                                    this.Img_PDZ.Visible = true;
                                }
                                this.IsReadonly = true;
                                this.FenChang.Enabled = false;
                                this.ChanPin.Enabled = false;
                                this.Date1.Enabled = false;
                                this.XuHao.Enabled = false;
                                this.KuQu.Enabled = false;
                                this.Button_Save.Enabled = false;
                                this.Button_Save.Visible = false;
                            }

                            Reader.Close();
                            Cmd.CommandText = "select 原料供货ID,rtrim(炉号) as 炉号,捆号,重量,长度,支数 from 产品库存 where 入库单ID=" + bid;
                            Cmd.CommandType = CommandType.Text;
                            Reader = Cmd.ExecuteReader();
                            string kh;
                            while (Reader.Read())
                            {
                                this.GHList.Value += ((int)Reader["原料供货ID"]).ToString() + "|";
                                this.LHList.Value += (string)Reader["炉号"] + "|";
                                kh = ((int)Reader["捆号"]).ToString();
                                kh=kh.PadLeft(9,'0');
                                this.KHList.Value += kh.Substring(0, 4) + "-" + kh.Substring(5) + "|";
                                this.ZLList.Value += ((decimal)Reader["重量"]).ToString() + "|";
                                this.CDList.Value += ((decimal)Reader["长度"]).ToString() + "|";
                                this.ZSList.Value += ((byte)Reader["支数"]).ToString() + "|";
                            }
                        }
                    }
                    Reader.Close();

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                    this.FenChang.Enabled = false;
                    this.ChanPin.Enabled = false;
                }
                else
                {
                    this.Label_ZhiBiao.Text = (String)Session["OperatorName"];
                    if (DateTime.Now.Hour > 12)
                    {
                        this.Date1.Text = DateTime.Today.ToString("yyyy年M月d日");
                    }
                    else
                    {
                        this.Date1.Text = DateTime.Today.AddDays(-1).ToString("yyyy年M月d日");
                    }
                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            DateTime d;
            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.Notice = "“日期”录入有误。";
                return;
            }

            int xh;
            if (!int.TryParse(this.XuHao.Text, out xh))
            {
                this.Notice = "“序号”录入有误。";
                return;
            }

            int kq;
            if (!int.TryParse(this.KuQu.Text, out kq))
            {
                this.Notice = "“库区”录入有误。";
                return;
            }
            else if (kq <= 0 || kq > 100)
            {
                this.Notice = "“库区”需录 1 - 100 的正整数。";
                return;
            }

            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }

            string[] ghlist = this.GHList.Value.Split(new char[] { '|' });
            string[] lhlist = this.LHList.Value.Split(new char[] { '|' });
            string[] khlist = this.KHList.Value.Split(new char[] { '|' });
            string[] zllist = this.ZLList.Value.Split(new char[] { '|' });
            string[] cdlist = this.CDList.Value.Split(new char[] { '|' });
            string[] zslist = this.ZSList.Value.Split(new char[] { '|' });

            int gh = 0,gh1=0, kh, zs, zs2 = 0, n1, n2;
            string lh = "", lh1 = "";
            decimal zl, cd;

            string sql = "";

            string[] tmp;

            for (int i = 0; i <60; i++)
            {
                if (i > ghlist.Length - 1 || i > lhlist.Length - 1 || i > khlist.Length - 1
                    || i > zllist.Length - 1 || i > cdlist.Length - 1 || i > zslist.Length - 1)
                {
                    break;
                }

                if (khlist[i] != "" || zllist[i] != "" || cdlist[i] != "" || zslist[i] != "")
                {
                    tmp=khlist[i].Split(new char[] { '-' });

                    if (tmp.Length != 2 || !int.TryParse(tmp[0], out n1) || !int.TryParse(tmp[1], out n2))
                    {
                        this.Notice = "第 " + (i + 1) + " 个捆数编号录入有误";
                        return;
                    }
                    else
                    {
                        kh = n1 * 100000 + n2;
                    }
                    this.Notice = "第 " + (i + 1) + " 个捆重量录入有误";
                    if (!decimal.TryParse(zllist[i], out zl))
                    {
                        return;
                    }
                    else if (zl <= 0)
                    {
                        return;
                    }
                    this.Notice = "第 " + (i + 1) + " 个捆长度录入有误";
                    if (!decimal.TryParse(cdlist[i], out cd))
                    {
                        return;
                    }
                    else if (cd <= 0)
                    {
                        return;
                    }
                    this.Notice = "第 " + (i + 1) + " 个支数录入有误";
                    if (!int.TryParse(zslist[i], out zs))
                    {
                        return;
                    }
                    else if (zs <= 0)
                    {
                        return;
                    }

                    if (ghlist[i]!="" && int.TryParse(ghlist[i], out gh))
                    {
                        if (gh1 == 0) gh1 = gh;
                        sql += "set @供货ID=" + gh + ";";
                    }

                    if (lhlist[i].Trim() != "")
                    {
                        lh = lhlist[i];
                        if (lh1 == "") lh1 = lh;
                        sql += "set @炉号='" + lhlist[i].Replace("'", "''") + "';";
                    }
                    if (gh == 0)
                    {
                        this.Notice = "未选择原料供货商";
                        return;
                    }
                    if (lh == "")
                    {
                        this.Notice = "炉号未填写";
                        return;
                    }
                    sql += "set @捆号=" + kh + ";";
                    sql += "set @重量=" + zl + ";";
                    sql += "set @长度=" + cd + ";";
                    if (zs != zs2)
                    {
                        zs2 = zs;
                        sql += "set @支数=" + zs + ";";
                    }
                    this.ZZL += zl;
                    this.ZCD += cd;
                    this.ZZS += zs;
                    this.ZJS++;
                    sql += "insert into 产品库存(入库单ID,产品类别ID,生产线,原料供货ID,炉号,捆号,重量,长度,支数,生产日期,入库日期,库区)"
                        + "values(@入库ID,@产品ID,@生产线,@供货ID,@炉号,@捆号,@重量,@长度,@支数,@日期,@日期,@库区);"
                        + "if not exists(select * from 产品理化参数 where 产品ID=@产品ID and 炉号=@炉号 and 供货ID=@供货ID) begin "
                        + "delete 产品理化参数 where 产品ID=@产品ID and 炉号=@炉号 "
                        +" insert into 产品理化参数([产品ID],[供货ID],[炉号],[C],[Mn],[Si],[P],[S],[Cr],[Ni],[Cu],[Mo],[Al],[V],[Bi],[Sn],[Pb],[As],[Sb],[抗拉强度],[屈服极限],[伸长率],[横向冲击],[纵向冲击],[通径检验],[静水压试验],[无损探伤],[表面检验],[尺寸])"
                        + "VALUES(@产品ID,@供货ID,@炉号,'','','','','','','','','','','','','','','','','','','','','','','','','','') end;";                                            
               }
            }
            if (this.ZJS <= 0)
            {
                this.Notice = "·保存失败，您需要录入更多有效的信息。";
                return;
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            Cmd.CommandText = "declare @炉号 char(10);declare @备注 nvarchar(50);";
            Cmd.CommandText = Cmd.CommandText + "set @炉号='" + lh1.Replace("'", "''") + "';";
            Cmd.CommandText = Cmd.CommandText + "set @备注='" + this.BeiZhu.Value.Replace("'", "''") + "';";

            if (this.Label_ID.Text == "0" || this.Label_ID.Text == "")
            {
                Cmd.CommandText = Cmd.CommandText + "insert into 产品入库单(序号,产品类别ID,供货单位ID,生产线,炉号,日期,库区,总件数,总支数,总重量,总长度,制表人,备注)values("
                    + xh + "," + this.ChanPin.Text + "," + gh1 + "," + this.FenChang.Text + ",@炉号,'"
                    +d.ToShortDateString()+"',"+kq+","+this.ZJS+","+this.ZZS+","+this.ZZL+","+this.ZCD
                    +",'"+op+"',@备注);select cast(@@identity as int);";

                this.Label_ID.Text = ((int)Cmd.ExecuteScalar()).ToString();
                Cmd.CommandText = "set nocount on;"
                    + "declare @入库ID int,@产品ID int,@生产线 smallint,@供货ID int,@炉号 char(10),@捆号 int,@重量 numeric(9,1),@长度 numeric(9,2),@支数 tinyint,@日期 date,@库区 tinyint;"
                    + "set @入库ID=" + this.Label_ID.Text + ";"
                    + "set @产品ID=" + this.ChanPin.Text + ";"
                    + "set @生产线=" + this.FenChang.Text + ";"
                    + "set @日期='"+d.ToShortDateString()+"';"
                    + "set @库区=" + kq + ";"
                    + sql;
                Cmd.ExecuteNonQuery();
            }
            else if (int.TryParse(this.Label_ID.Text, out n1) && n1 > 0)
            {
                Cmd.CommandText = Cmd.CommandText + "update 产品入库单 set 序号="+xh
			        +",产品类别ID="+this.ChanPin.Text
			        +",供货单位ID="+gh1
			        +",生产线="+  this.FenChang.Text
			        +",炉号=@炉号"
			        +",日期='"+d.ToShortDateString()+"'"
			        +",库区="+kq
			        +",总件数="+this.ZJS
                    +",总支数="+this.ZZS
                    +",总重量="+this.ZZL
                    +",总长度="+this.ZCD
			        +",制表人='"+op+"'"
                    + ",备注=@备注 where ID=" + this.Label_ID.Text
                    + ";delete from 产品库存 where 入库单ID=" + this.Label_ID.Text;
                Cmd.ExecuteNonQuery();
                Cmd.CommandText = "set nocount on;"
                    + "declare @入库ID int,@产品ID int,@生产线 smallint,@供货ID int,@炉号 char(10),@捆号 int,@重量 numeric(9,1),@长度 numeric(9,2),@支数 tinyint,@日期 date,@库区 tinyint;"
                    + "set @入库ID=" + this.Label_ID.Text + ";"
                    + "set @产品ID=" + this.ChanPin.Text + ";"
                    + "set @生产线=" + this.FenChang.Text + ";"
                    + "set @日期='" + d.ToShortDateString() + "';"
                    + "set @库区=" + kq + ";"
                    + sql;
                Cmd.ExecuteNonQuery();
            }

            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
            this.FenChang.Enabled = false;
            this.ChanPin.Enabled = false;
            this.Notice = "·保存成功";


        }
    }
}
